package com.dragon.speech.common.config;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.jetty.JettyServerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by <a href='http://yulin.io'>hyl<a> on 2017/6/3 14:13.
 */
@Configuration
public class JettyConfig {
    private static Logger logger = LoggerFactory.getLogger(JettyConfig.class);

    @Bean
    public EmbeddedServletContainerFactory servletContainerFactory(
            @Value("${jetty.threadPool.maxThreads:256}") final String maxThreads,
            @Value("${jetty.threadPool.minThreads:8}") final String minThreads,
            @Value("${jetty.threadPool.idleTimeout:120000}") final String idleTimeout) {

        JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory = new JettyEmbeddedServletContainerFactory();
        jettyEmbeddedServletContainerFactory.addServerCustomizers(new JettyServerCustomizer() {

            @Override
            public void customize(Server server) {
                logger.info("==== jetty config maxThreads:{} minThreads:{} idleTimeout:{}", maxThreads, minThreads, idleTimeout);
                final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class);
                threadPool.setMaxThreads(Integer.valueOf(maxThreads));
                threadPool.setMinThreads(Integer.valueOf(minThreads));
                threadPool.setIdleTimeout(Integer.valueOf(idleTimeout));
            }
        });

        return jettyEmbeddedServletContainerFactory;
    }

}
